var actionPath='/system/resource/';
var rootActionPath=parent.CONTEXT_PATH+actionPath;
var currentNode;
jQuery(function($){

	$("#dg").treegrid({
		url : rootActionPath+'queryResourcesByModuleId.xhtml',
		rownumbers:true,
        method:'post',
        fit:true,
        idField: 'id',
        toolbar: '#toolbar',
        singleSelect:true,
        fitColumns:true,
        treeField: 'text',
        remoteSort:true,
        nowrap: false, 
		striped: true, 
        border:false,
        queryParams:{
    		"moduleId": 0
    	}
	});
	
	
	
	$('#menuTree').tree({
	    url: parent.CONTEXT_PATH+'/system/module/queryAll.xhtml',
	    onSelect : function(node){
	    	
	    	if($('#menuTree').tree("getChildren",node.target).length){
	    		$('#addBut').linkbutton("disable");
				//$('#updateBut').linkbutton("disable");
				//$('#delBut').linkbutton("disable");
	    	}else{
	    		$('#addBut').linkbutton("enable");
				$('#updateBut').linkbutton("enable");
				$('#delBut').linkbutton("enable");
	    	}
	    	
	    	currentNode = node;
	    	$('#dg').treegrid({queryParams:{"moduleId": node.id}});
	    }, 
		//加载成功选中第一行
	    onLoadSuccess : function(node,data){
	    	try{
	    		//第一个元素的子元素的第一个元素选中
	    		var node = $(this).tree('find', data[0].children[0].id);
				$(this).tree('select', node.target);
	    	}catch(e){}
			
		}
	});
	
});


function add() {
	
	var node = $('#menuTree').tree("getSelected");
	if(!node){
		parent.showWarningBox(I18N.selectModuleError);
		return;
	}

	parent.$.modalDialog({
		title : I18N.addTitle,
		width : 401,
		height : 318,
		href : rootActionPath+'goCreate.xhtml?moduleId='+node.id,
		buttons : [ {
			text : GLOBAL_I18N.global_button_confirm,
			handler : function() {
				parent.$.modalDialog.openner_treeGrid = $('#dg');
				var f = parent.$.modalDialog.handler.find('#form');
				f.submit();
			}
		},
		
		{
			text : GLOBAL_I18N.global_button_cancel,
			handler : function() {
				parent.$.modalDialog.handler.dialog('close');
			}
		} 
		
		]
	});
}

	
	
	
function del(){
	var row = $('#dg').treegrid('getSelected');
	if(!row){
		parent.showWarningBox(Audaque.format(GLOBAL_I18N.global_message_please_select_remove,I18N.moduleName)); 
		return;
	}
	
	parent.showConfirmBox(Audaque.format(GLOBAL_I18N.global_message_remove_ask,I18N.moduleName), function(b) {
		if (b) {
			parent.postReturnJson(actionPath+'remove.xhtml',
					{id : row.pid ? row.pid:row.id},
					function(result){
						if (result.success) {
					    	$('#dg').treegrid({queryParams:{"moduleId": currentNode.id}});
						}
					}
			);
		}
	});
}


function edit(){

	var row = $('#dg').treegrid('getSelected');
	if (!row){
		parent.showWarningBox(Audaque.format(GLOBAL_I18N.global_message_please_select_edit,I18N.moduleName));
		return;
	}
	
	parent.$.modalDialog({
		title : I18N.updateTitle,
		width : 401,
		height : 315,
		href : rootActionPath+'goUpdate.xhtml?id='+(row.pid ? row.pid : row.id),
		buttons : [ {
			text : GLOBAL_I18N.global_button_confirm,
			handler : function() {
				parent.$.modalDialog.openner_treeGrid = $('#dg');
				var f = parent.$.modalDialog.handler.find('#form');
				f.submit();
			}
		},
		
		{
			text : GLOBAL_I18N.global_button_cancel,
			handler : function() {
				parent.$.modalDialog.handler.dialog('close');
			}
		} 
		
		]
	});
}

	